package com.estate.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.admin.service.AdminService;
import com.estate.bean.Manager;
import com.estate.mapper.ManagerMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements AdminService {
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Manager> list = list(wrapper);
        PageInfo<Manager> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveManager(Manager manager) {
        manager.setStatus(1);
        manager.setIsDelete(0);
        log.info("新增manager数据:{}", manager);
        QueryWrapper<Manager> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("login_name", manager.getLoginName());
        Manager manager1 = getOne(wrapper1);
        System.out.println(manager1);
        if (manager1 != null) {
            return ResultTool.fail(501, "登录名已经被使用啦");
        }

        QueryWrapper<Manager> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("name", manager.getName()).or();
        Manager manager2 = getOne(wrapper2);
        System.out.println(manager2);
        if (manager2 != null) {
            return ResultTool.fail(501, "真实姓名已经被使用啦");
        }

        QueryWrapper<Manager> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("telephone", manager.getTelephone());
        Manager manager3 = getOne(wrapper3);
        System.out.println(manager3);
        if (manager3 != null) {
            return ResultTool.fail(501, "手机号已经被使用啦");
        }


        QueryWrapper<Manager> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("email", manager.getEmail());
        Manager manager4 = getOne(wrapper4);
        System.out.println(manager4);
        if (manager4 != null) {
            return ResultTool.fail(501, "邮箱已经被使用啦");
        }

        boolean b = save(manager);
        if (!b) {
            return ResultTool.fail(501, "新增管理员失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateManager(Manager manager) {
        log.info("修改manager数据:{}", manager);
        boolean b = updateById(manager);
        if (!b) {
            return ResultTool.fail(503, "修改管理员失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteManager(String mangerId) {
        log.info("删除manager数据:{}", mangerId);
        boolean b = removeById(mangerId);
        if (!b) {
            return ResultTool.fail(502, "删除管理员失败");
        }
        return ResultTool.success("success");
    }
}
